package com.geeksx.trainer.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.geeksx.trainer.entity.User;
import com.geeksx.trainer.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Copyright (C), 2025, 北京同创永益科技发展有限公司
 *
 * @Classname CustomUserDetailsService
 * @Description TODO
 * @Date 2025/9/9 11:23
 * @Created by zhengkaixuan
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return org.springframework.security.core.userdetails.User
                .withUsername(user.getUsername())
                .password(user.getPassword())
                .roles("USER")
                .build();
    }
}
